package com.no1ikun.view;

import com.no1ikun.service.StudentService;
import com.no1ikun.service.StudentServiceImpl;
import com.no1ikun.util.MyButton;
import com.no1ikun.util.MyJLabel;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DeleteJPanel extends JPanel {
    private MyJTextField deleteInfo;
    private MyButton delete;
    private StudentService ss = new StudentServiceImpl();

    public DeleteJPanel() {
        //初始化数据
        init();
    }


    public void init() {
        this.setLayout(null);//绝对布局
        this.setBounds(80, 80, 885, 452);
        this.setBackground(Color.WHITE);

        MyJLabel text = new MyJLabel("请输入学号:", 200, 30, 60, 185, "微软雅黑", Font.BOLD, 17, new Color(28, 136, 201));
        deleteInfo = new MyJTextField(160, 187, 200, 25);
        deleteInfo.setBorder(new LineBorder(new Color(28, 136, 201)));

        //添加搜索按钮
        delete = new MyButton("删除", 380, 187, 60, 25, new Color(28, 136, 201), Color.WHITE);
        this.add(text);
        this.add(deleteInfo);
        this.add(delete);

        //绑定删除按钮的点击事件
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //接收前端的数据
                String text1 = deleteInfo.getText();
                String message = "";
                if (!"".equals(text1)) {
                    boolean b = ss.checkedStudent(text1);
                    if (b) {
                        int option = JOptionPane.showConfirmDialog(null, "确定要删除这个用户吗?", "温馨提示", JOptionPane.YES_NO_OPTION);
                        //判断用户选择的是哪个按钮
                        if (option == JOptionPane.YES_OPTION) {
                            int i = ss.deleteStudentBySno(text1);
                            message = i == 0 ? "删除失败,请检查" : "恭喜删除成功";
                            //给出友好提示?
                            JOptionPane.showMessageDialog(null, message, "温馨提示", JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        message = "对不起,没有这个用户!";
                        //给出友好提示?
                        JOptionPane.showMessageDialog(null, message, "温馨提示", JOptionPane.WARNING_MESSAGE);
                    }


                }

            }
        });


    }
}
